<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/17
 * Time: 10:51
 */
namespace app\api\controller;

use think\Db;

class Apilogin extends Base
{
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
    }
    
    /**
     * 注册
     * @return bool
     * phone  手机号
     * code  验证码
     * password  密码
     * surePassword  确认密码
     */
    public function userRegister()
    {
        $phone = input('phone');//手机号
        $code = input('code');//验证码
        $password = trim(input('password'), ' ');//密码
        $surePassword = trim(input('surePassword'), ' ');//确认密码
        
        if($phone == '' || $code == '' || $password == '' || $surePassword == '') {
            return json(['status' => 0, 'msg' => '有未填项，请检查']);
        }
        //验证手机号
//        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
        $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(!empty($username)) {
            return json(['status' => 0, 'msg' => '此手机号码已注册']);
        }
        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('create_time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        $create_time = strtotime($res['create_time']);
        if($now - $create_time >= 601) {
            return json(['status' => 0, 'msg' => '验证码失效']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        //验证密码
        if($password != $surePassword) {
            return json(['status' => 0, 'msg' => '两次输入的密码不一致，请重新输入']);
        }
        $send_coin = Db::table('up_about_us')->where('about_id',3)->value('super_time');
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $data = [
            'user_phone' => $phone,
            'user_token' => $token,
            'user_time' => date('Y-m-d H:i:s'),//注册时间
            'user_password' => md5($password),
            'last_login_time' => date('Y-m-d H:i:s'),//最后登录时间
            'user_mobile' => $this->androidios(),//android  or ios
            'user_username' => '云屏' . rand(10000,99999), //昵称默认云屏+随机五位数
            'user_china' => input('ex'),
            'user_recharge' => $send_coin
        ];
        $user_id = Db::name('u_user')->insertGetId($data);
        //注册成功后自动登录
        if($user_id) {
            allInDetail($user_id,1,'注册送云币',$send_coin,10);
            Db::table('up_phonecode')->where('id', $res['id'])->setField('status', 1);
            $userInfo = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->find();
            return json(['status' => 200, 'msg' => $userInfo]);
        } else {
            return json(['status' => 0, 'msg' => '注册失败']);
        }
    }
    
    /**
     * 账号密码登录
     * @return bool
     * phone  手机号
     * password  密码
     */
    public function userLogin()
    {
        $phone = input('phone');
        $password = trim(input('password'));
        if($phone == '') {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }
        if($password == '') {
            return json(['status' => 0, 'msg' => '请输入密码']);
        }
//        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($userInfo)) {
            return json(['status' => 0, 'msg' => '该手机号尚未注册']);
        }
        if(md5($password) != $userInfo['user_password']) {
            return json(['status' => 0, 'msg' => '密码错误']);
        }
        if($userInfo['user_status'] == 2) {
            return json(['status' => 0, 'msg' => '该账号已被冻结']);
        }
        if($userInfo['user_status'] == 3) {
            return json(['status' => 0, 'msg' => '该账号已被删除，请联系管理员']);
        }
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $update = [
            'user_token' => $token,
            'last_login_time' => date('Y-m-d H:i:s'),
        ];
        Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
        $user['third_type'] = 5;
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 快捷登录
     * @return bool
     * phone  手机号
     * code  验证码
     */
    public function phoneLogin()
    {
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        
        if($phone == '') {
            return json(['status' => 0, 'msg' => '请输入手机号码']);
        }
        if($code == '') {
            return json(['status' => 0, 'msg' => '请输入验证码']);
        }
//        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($userInfo)) {
            return json(['status' => 0, 'msg' => '手机号未注册，请先去注册']);
        }
        
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('create_time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        $create_time = strtotime($res['create_time']);
        if($now - $create_time >= 601) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        
        if($userInfo['user_status'] == 2) {
            return json(['status' => 0, ' msg' => '该账号已被冻结']);
        }
        if($userInfo['user_status'] == 3) {
            return json(['status' => 0, ' msg' => '该账号已被删除，请联系管理员']);
        }
        //更新token
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $update = [
            'user_token' => $token,
            'last_login_time' => date('Y-m-d H:i:s')
        ];
        Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
        Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
        return json(['status' => 200, 'msg' => $username]);
    }
    
    /**
     * 微信||QQ||支付宝||微博  第一步
     * thirdToken  第三方token
     * type  第三方类型   1 微信  2 qq  3 微博  4 支付宝
     * @return string
     */
    public function thirdLogin()
    {
        $thirdToken = input('thirdToken');
        $type = input('type');
        if($type == 1) {
            $field = '_wx_';
        } elseif($type == 2) {
            $field = '_qq_';
        } elseif($type == 3) {
            $field = '_wb_';
        } else {
            $field = '_zfb_';
        }
        $where["user{$field}token"] = $thirdToken;
        $isFirstLogin = Db::table('up_u_user')->where($where)->find();
        //目前的逻辑----不管是不是第一次登录都可以直接登录，不需要绑定手机号，在第二次领取任务时绑定手机号
        if($isFirstLogin) {
            $isFirstLogin['third_type'] = $type;
            return json(['status' => 200, 'msg' => $isFirstLogin]);
        } else {
            return json(['status' => 0, 'msg' => []]);
        }
    }
    
    /**
     * 第三方登录直接插入数据库
     * 直接插入数据库--登录
     *
     * thirdToken  第三方token
     * name  第三方昵称
     * image  第三方头像
     * type  第三方类型   1 微信  2 qq  3 微博  4 支付宝
     * @return string
     */
    public function isNotBing(){
        $type = input('type');
        if($type == 1) {
            $field = '_wx_';
        } elseif($type == 2) {
            $field = '_qq_';
        } elseif($type == 3) {
            $field = '_wb_';
        } else {
            $field = '_zfb_';
        }
        $thirdToken = input('thirdToken');
        $name = input('name');
        $image = input('image');
//        $send_coin = Db::table('up_about_us')->where('about_id',3)->value('super_time');
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $insert = [
            'user_token' => $token,
            'user' . $field . 'token' => $thirdToken,
            'user' . $field . 'nickname' => $name,
            'user' . $field . 'image' => $image,
            'last_login_time' => date('Y-m-d H:i:s'),
            'user_username' => $name,
            'user_image' => $image,
//            'user_recharge' => $send_coin
        ];
        
        $user_id = Db::table('up_u_user')->insertGetId($insert); // 插入数据库
        if($user_id) {
//            allInDetail($user_id,1,'注册送云币',$send_coin,10);
            $userData = Db::table('up_u_user')->where('user_id', $user_id)->find();
            $userData['third_type'] = $type;
            return json(['status' => 200, 'msg' => $userData]);
        } else {
            return json(['status' => 0, 'msg' => '登录失败']);
        }
    }
    
    /**
     * 微信||QQ||支付宝||微博   第二步
     * thirdToken  第三方token
     * phone  手机号
     * code  验证码
     * name  第三方昵称
     * image  第三方头像
     * type  第三方类型   1 微信  2 qq  3 微博  4 支付宝
     * @return string
     */
    public function thirdLoginS(){
        $type = input('type');
        if($type == 1) {
            $field = '_wx_';
        } elseif($type == 2) {
            $field = '_qq_';
        } elseif($type == 3) {
            $field = '_wb_';
        } else {
            $field = '_zfb_';
        }
        $thirdToken = input('thirdToken');
        $name = input('name');
        $image = input('image');
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        if($code == '' || $phone == '') {
            return json(['status' => 0, 'msg' => '有未填项，请检查']);
        }
//        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请填写正确的手机号码']);
//        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($userInfo)) {
            return json(['status' => 0, 'msg' => '该手机号未注册，请先注册']);
        }
        if($userInfo['user_status'] == 2) {
            return json(['status' => 0, ' msg' => '该账号已被冻结']);
        }
        if($userInfo['user_status'] == 3) {
            return json(['status' => 0, ' msg' => '该账号已被删除，请联系管理员']);
        }
        if($userInfo["user{$field}token"]) {
            return json(['status' => 0, 'msg' => '该手机号已被绑定，需解绑后重新绑定']);
        }
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('create_time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        $create_time = strtotime($res['create_time']);
        if($now - $create_time >= 601) {
            return json(['status' => 0, 'msg' => '验证码失效']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
    
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $update = [
            'user_token' => $token,
            'user' . $field . 'token' => $thirdToken,
            'user' . $field . 'nickname' => $name,
            'user' . $field . 'image' => $image,
            'last_login_time' => date('Y-m-d H:i:s')
        ];
        //如果用户没有昵称、头像信息，则更新为第三方昵称、头像
        if(!$userInfo['user_username']) {
            $update['user_username'] = $name;
        }
        if(!$userInfo['user_image']) {
            $update['user_image'] = $image;
        }
        $res1 = Db::table('up_u_user')->where('user_phone', $phone)->update($update);//更新用户信息
        if($res1) {
            $userData = Db::table('up_u_user')->where('user_phone', $phone)->find();
            $userData['third_type'] = $type;
            Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);//改变验证码状态
            return json(['status' => 200, 'msg' => $userData]);
        } else {
            return json(['status' => 0, 'msg' => '绑定失败']);
        }
    }
    
    /**
     * 忘记密码
     * phone  手机号
     * code  验证码
     * newpass  新密码
     * sureNewpass  确认新密码
     * @return bool
     */
    public function forgetPass()
    {
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        $newpass = trim(input('newpass'), ' ');
        $sureNewpass = trim(input('sureNewpass'), ' ');
        if($phone == '' || $code == '' || $newpass == '' || $sureNewpass == '') {
            return json(['status' => 0, 'msg' => '有未填项']);
        }
//        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
        $username = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($username)) {
            return json(['status' => 0, 'msg' => '当前手机号未被注册']);
        }
        
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('create_time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        $create_time = strtotime($res['create_time']);
        if($now - $create_time >= 601) {
            return json(['status' => 0, 'msg' => '验证码失效']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        if($newpass != $sureNewpass) {
            return json(['status' => 0, 'msg' => '两次输入的密码不一致请重新输入']);
        }
        
        $update['user_password'] = md5($newpass);
        $result = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        if($result) {
            Db::table('up_phonecode')->where('id', $res['id'])->update(['status' => 1]);
            return json(['status' => 200, 'msg' => '重置成功']);
        } else {
            return json(['status' => 0, 'msg' => '重置失败']);
        }
    }
    
    /**
     * 用户注册协议
     * @return \think\response\Json
     */
    public function userAgreement()
    {
        $agreement = Db::table('up_about_us')
            ->where('about_id', 1)
            ->value('agreement');
        if($agreement) {
            return json(['status' => 200, 'msg' => $agreement]);
        } else {
            return json(['status' => 0, 'msg' => []]);
        }
    }
    
    /**
     * 用户协议和隐私协议
     * @return \think\response\Json
     */
    public function userPlusTwo()
    {
        $type = $_POST['type'];
        if($type == 1){
            $agreement = Db::table('up_about_us')
                ->where('about_id', 1)
                ->value('user_agreement');
        }else{
            $agreement = Db::table('up_about_us')
                ->where('about_id', 1)
                ->value('user_priavcy');
        }

        if($agreement) {
            return json(['status' => 200, 'msg' => $agreement]);
        } else {
            return json(['status' => 0, 'msg' => []]);
        }
    }
    /**
     * 提现说明
     * @return \think\response\Json
     */
    public function moneyOutRule()
    {
        $agreement = Db::table('up_about_us')
            ->where('about_id', 1)
            ->value('withdraw_explain');
        if($agreement) {
            return json(['status' => 200, 'msg' => $agreement]);
        } else {
            return json(['status' => 0, 'msg' => []]);
        }
    }
    
    /**
     * 重复绑定手机号时展示的列表
     * @return \think\response\Json
     * phone   被绑定的手机号
     */
    public function showUser(){
        $params = input('param.');
        // 重复绑定的手机号记录
        $userList = Db::table('up_u_user')
            ->field('user_id, user_image, user_phone')
            ->where('user_phone', $params['phone'])
            ->find();
        return json(['status' => 200, 'msg' => $userList]);
    }
    
    /**
     * 验证冲突账号
     * @return \think\response\Json
     * phone   手机号
     * code    验证码
     */
    public function checkPhone(){
        $params = input('param.');
        $res = Db::table('up_phonecode')
            ->where('phone', $params['phone'])
            ->order('create_time desc')
            ->limit(1)
            ->find();
        if($params['code'] != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        $create_time = strtotime($res['create_time']);
        if($now - $create_time >= 601) {
            return json(['status' => 0, 'msg' => '验证码失效']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        return json(['status' => 200, 'msg' => true]);
    }
    
    /**
     * 注销----第三方登录时注销
     * @return \think\response\Json
     * del_user_id    被注销账号的ID
     * type           本次第三方登录的类型   1 微信  2 qq  3 微博  4 支付宝
     * thirdToken     本次第三方登录的token
     * thirdName      本次第三方登录的昵称
     * thirdImg       本次第三方登录的头像
     */
    public function delUser(){
        $del_user_id = input('del_user_id');
        $type = input('type');
        $thirdToken = input('thirdToken');
        $thirdName = input('thirdName');
        $thirdImg = input('thirdImg');
        
        // 取出原账号的账号信息：微信、QQ、微博、支付宝账号、手机号
        $info = Db::table('up_u_user')
            ->where('user_id', $del_user_id)
            ->field('user_phone, user_wx_token, user_wx_nickname, user_wx_image, user_qq_token, 
            user_qq_nickname, user_qq_image, user_wb_token, user_wb_nickname, user_wb_image, 
            user_zfb_token, user_zfb_nickname, user_zfb_image, user_password')
            ->find();
        if($type == 1){ // 微信
            $data = [
                'user_password' => $info['user_password'],
                'user_phone' => $info['user_phone'],
                'user_wx_token' => $thirdToken,
                'user_wx_nickname' => $thirdName,
                'user_wx_image' => $thirdImg,
                'user_qq_token' => $info['user_qq_token'],
                'user_qq_nickname' => $info['user_qq_nickname'],
                'user_qq_image' => $info['user_qq_image'],
                'user_wb_token' => $info['user_wb_token'],
                'user_wb_nickname' => $info['user_wb_nickname'],
                'user_wb_image' => $info['user_wb_image'],
                'user_zfb_token' => $info['user_zfb_token'],
                'user_zfb_nickname' => $info['user_zfb_nickname'],
                'user_zfb_image' => $info['user_zfb_image']
            ];
        }elseif($type == 2){ // QQ
            $data = [
                'user_password' => $info['user_password'],
                'user_phone' => $info['user_phone'],
                'user_wx_token' => $info['user_wx_token'],
                'user_wx_nickname' => $info['user_wx_nickname'],
                'user_wx_image' => $info['user_wx_image'],
                'user_qq_token' => $thirdToken,
                'user_qq_nickname' => $thirdName,
                'user_qq_image' => $thirdImg,
                'user_wb_token' => $info['user_wb_token'],
                'user_wb_nickname' => $info['user_wb_nickname'],
                'user_wb_image' => $info['user_wb_image'],
                'user_zfb_token' => $info['user_zfb_token'],
                'user_zfb_nickname' => $info['user_zfb_nickname'],
                'user_zfb_image' => $info['user_zfb_image']
            ];
        }elseif($type == 3){ // 微博
            $data = [
                'user_password' => $info['user_password'],
                'user_phone' => $info['user_phone'],
                'user_wx_token' => $info['user_wx_token'],
                'user_wx_nickname' => $info['user_wx_nickname'],
                'user_wx_image' => $info['user_wx_image'],
                'user_qq_token' => $info['user_qq_token'],
                'user_qq_nickname' => $info['user_qq_nickname'],
                'user_qq_image' => $info['user_qq_image'],
                'user_wb_token' => $thirdToken,
                'user_wb_nickname' => $thirdName,
                'user_wb_image' => $thirdImg,
                'user_zfb_token' => $info['user_zfb_token'],
                'user_zfb_nickname' => $info['user_zfb_nickname'],
                'user_zfb_image' => $info['user_zfb_image']
            ];
        }else{ // 支付宝
            $data = [
                'user_password' => $info['user_password'],
                'user_phone' => $info['user_phone'],
                'user_wx_token' => $info['user_wx_token'],
                'user_wx_nickname' => $info['user_wx_nickname'],
                'user_wx_image' => $info['user_wx_image'],
                'user_qq_token' => $info['user_qq_token'],
                'user_qq_nickname' => $info['user_qq_nickname'],
                'user_qq_image' => $info['user_qq_image'],
                'user_wb_token' => $info['user_wb_token'],
                'user_wb_nickname' => $info['user_wb_nickname'],
                'user_wb_image' => $info['user_wb_image'],
                'user_zfb_token' => $thirdToken,
                'user_zfb_nickname' => $thirdName,
                'user_zfb_image' => $thirdImg
            ];
        }
        Db::startTrans();
        try{
            // 插入新数据
            $send_coin = Db::table('up_about_us')->where('about_id',3)->value('super_time');
            $data['user_recharge'] = $send_coin;
            $data['user_china'] = $info['user_china'];
            $user_id = Db::table('up_u_user')->insertGetId($data);
            $user_info = Db::table('up_u_user')->where('user_id', $user_id)->find();
            allInDetail($user_id,1,'注册送云币',$send_coin,10);
            // 删除原数据
            Db::table('up_u_user')->where('user_id', $del_user_id)->delete();
            $res = 1;
            Db::commit();
        }catch(\Exception $e){
            $user_info = [];
            $res = 2;
            Db::rollback();
        }
        
        if($res == 1){
            return json(['status' => 200, 'msg' => $user_info]);
        }else{
            return json(['status' => 0, 'msg' => $user_info]);
        }
    }
    
    /**
     * 注销----账号管理注销
     * @return \think\response\Json
     * del_user_id    被注销账号的ID
     * type           本次第三方登录的类型   1 微信  2 qq  3 微博  4 支付宝  5 手机
     */
    public function BingDelUser(){
        $del_user_id = input('del_user_id');
        $type = input('type');
        $user_id = input('user_id');
        // 取出原账号的账号信息：微信、QQ、微博、支付宝账号、手机号
        $info = Db::table('up_u_user')
            ->where('user_id', $del_user_id)
            ->field('user_phone, user_wx_token, user_wx_nickname, user_wx_image, user_qq_token, 
            user_qq_nickname, user_qq_image, user_wb_token, user_wb_nickname, user_wb_image, 
            user_zfb_token, user_zfb_nickname, user_zfb_image, user_password')
            ->find();
        // 现在登录的账号的信息
        $info_now = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_phone, user_wx_token, user_wx_nickname, user_wx_image, user_qq_token, 
            user_qq_nickname, user_qq_image, user_wb_token, user_wb_nickname, user_wb_image, 
            user_zfb_token, user_zfb_nickname, user_zfb_image, user_password')
            ->find();
        $data = [];
    
        if($type == 1){ // 微信
            if(!$info_now['user_password']){
                $data['user_password'] = $info['user_password'];
            }
            if(!$info_now['user_phone']){
                $data['user_phone'] = $info['user_phone'];
            }
            if(!$info_now['user_qq_token']){
                $data['user_qq_token'] = $info['user_qq_token'];
                $data['user_qq_nickname'] = $info['user_qq_nickname'];
                $data['user_qq_image'] = $info['user_qq_image'];
            }
            if(!$info_now['user_wb_token']){
                $data['user_wb_token'] = $info['user_wb_token'];
                $data['user_wb_nickname'] = $info['user_wb_nickname'];
                $data['user_wb_image'] = $info['user_wb_image'];
            }
            if(!$info_now['user_zfb_token']){
                $data['user_zfb_token'] = $info['user_zfb_token'];
                $data['user_zfb_nickname'] = $info['user_zfb_nickname'];
                $data['user_zfb_image'] = $info['user_zfb_image'];
            }
        }elseif($type == 2){ // QQ
            if(!$info_now['user_password']){
                $data['user_password'] = $info['user_password'];
            }
            if(!$info_now['user_phone']){
                $data['user_phone'] = $info['user_phone'];
            }
            if(!$info_now['user_wx_token']){
                $data['user_wx_token'] = $info['user_wx_token'];
                $data['user_wx_nickname'] = $info['user_wx_nickname'];
                $data['user_wx_image'] = $info['user_wx_image'];
            }
            if(!$info_now['user_wb_token']){
                $data['user_wb_token'] = $info['user_wb_token'];
                $data['user_wb_nickname'] = $info['user_wb_nickname'];
                $data['user_wb_image'] = $info['user_wb_image'];
            }
            if(!$info_now['user_zfb_token']){
                $data['user_zfb_token'] = $info['user_zfb_token'];
                $data['user_zfb_nickname'] = $info['user_zfb_nickname'];
                $data['user_zfb_image'] = $info['user_zfb_image'];
            }
        }elseif($type == 3){ // 微博
            if(!$info_now['user_password']){
                $data['user_password'] = $info['user_password'];
            }
            if(!$info_now['user_phone']){
                $data['user_phone'] = $info['user_phone'];
            }
            if(!$info_now['user_wx_token']){
                $data['user_wx_token'] = $info['user_wx_token'];
                $data['user_wx_nickname'] = $info['user_wx_nickname'];
                $data['user_wx_image'] = $info['user_wx_image'];
            }
            if(!$info_now['user_qq_token']){
                $data['user_qq_token'] = $info['user_qq_token'];
                $data['user_qq_nickname'] = $info['user_qq_nickname'];
                $data['user_qq_image'] = $info['user_qq_image'];
            }
            if(!$info_now['user_zfb_token']){
                $data['user_zfb_token'] = $info['user_zfb_token'];
                $data['user_zfb_nickname'] = $info['user_zfb_nickname'];
                $data['user_zfb_image'] = $info['user_zfb_image'];
            }
        }elseif($type == 4){ // 支付宝
            if(!$info_now['user_password']){
                $data['user_password'] = $info['user_password'];
            }
            if(!$info_now['user_phone']){
                $data['user_phone'] = $info['user_phone'];
            }
            if(!$info_now['user_wx_token']){
                $data['user_wx_token'] = $info['user_wx_token'];
                $data['user_wx_nickname'] = $info['user_wx_nickname'];
                $data['user_wx_image'] = $info['user_wx_image'];
            }
            if(!$info_now['user_qq_token']){
                $data['user_qq_token'] = $info['user_qq_token'];
                $data['user_qq_nickname'] = $info['user_qq_nickname'];
                $data['user_qq_image'] = $info['user_qq_image'];
            }
            if(!$info_now['user_wb_token']){
                $data['user_wb_token'] = $info['user_wb_token'];
                $data['user_wb_nickname'] = $info['user_wb_nickname'];
                $data['user_wb_image'] = $info['user_wb_image'];
            }
        }else{
            if(!$info_now['user_wx_token']){
                $data['user_wx_token'] = $info['user_wx_token'];
                $data['user_wx_nickname'] = $info['user_wx_nickname'];
                $data['user_wx_image'] = $info['user_wx_image'];
            }
            if(!$info_now['user_qq_token']){
                $data['user_qq_token'] = $info['user_qq_token'];
                $data['user_qq_nickname'] = $info['user_qq_nickname'];
                $data['user_qq_image'] = $info['user_qq_image'];
            }
            if(!$info_now['user_wb_token']){
                $data['user_wb_token'] = $info['user_wb_token'];
                $data['user_wb_nickname'] = $info['user_wb_nickname'];
                $data['user_wb_image'] = $info['user_wb_image'];
            }
            if(!$info_now['user_zfb_token']){
                $data['user_zfb_token'] = $info['user_zfb_token'];
                $data['user_zfb_nickname'] = $info['user_zfb_nickname'];
                $data['user_zfb_image'] = $info['user_zfb_image'];
            }
        }
        Db::startTrans();
        try{
            // 更新当前登录账号的数据
            Db::table('up_u_user')->where('user_id', $user_id)->update($data);
            $user_info = Db::table('up_u_user')->where('user_id', $user_id)->find();
            
            // 删除原数据
            Db::table('up_u_user')->where('user_id', $del_user_id)->delete();
            $res = 1;
            Db::commit();
        }catch(\Exception $e){
            $user_info = [];
            $res = 2;
            Db::rollback();
        }
        
        if($res == 1){
            return json(['status' => 200, 'msg' => $user_info]);
        }else{
            return json(['status' => 0, 'msg' => $user_info]);
        }
    }
    

    /**
     * 发送验证码
     * @return \think\response\Json|void
     * phone：填写的手机号
     * type：   1 验证手机号  2 第三方登录绑定手机号  3 注册
     * types  绑定的第三方类型  当type为2时，传此值  1微信  2QQ  3微博  4支付宝
     */
    public function getPhoneCode()
    {
        header('Access-Control-Allow-Origin:*');
        $params = input('param.');
        if($params['type'] == 6){
            $userInfo = Db::table('up_u_user')->where('user_id', $params['user_id'])->find();
            $params['phone'] = $userInfo['user_phone'];
        }else{
            $userInfo = Db::table('up_u_user')->where('user_phone', $params['phone'])->find();
        }
        if(trim($params['phone'], ' ') == '') {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }
//        if(!preg_match("/^1[3456789]\d{9}$/", $params['phone'])) {
//            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
//        }
        if(!isset($params['hh'])){
            if(!isset($_SERVER['HTTP_REQDATE']) || !isset($_SERVER['HTTP_REQKEY'])){
                return json(['status' => 0, 'msg' => '参数不全']);
            }
            $time = $_SERVER['HTTP_REQDATE'];
            $reqKey = $_SERVER['HTTP_REQKEY'];
            $appId = "A6097314776331";
            $appkey = "6A3C022C-9171-00A1-943B-C9675EE1C3E0";
            $myreqKey = md5($appId . 'XW' . $appkey . 'XW' . $time);
            if($myreqKey != $reqKey){
                return json(['status' => 0, 'msg' => '非法来源']);
            }
        }

        if(!empty($userInfo) && $params['type'] == 3) {//注册
            return json(['status' => 0, 'msg' => '该手机号已注册']);
        }
        if(empty($userInfo) && $params['type'] == 1) {//验证手机号
            return json(['status' => 0, 'msg' => '手机号未注册']);
        }
        // 第三方登录绑定手机号时，需要判断该手机号是否已存在，如果存在，则进入选择注销记录
        if(!empty($userInfo) && $params['type'] == 2) {//第三方登录时绑定手机号
            return json(['status' => 410, 'msg' => $userInfo]);
//            if($params['types'] == 1) {
//                $field = '_wx_';
//            } elseif($params['types'] == 2) {
//                $field = '_qq_';
//            } elseif($params['types'] == 3) {
//                $field = '_wb_';
//            }else{
//                $field = '_zfb_';
//            }
//            if($userInfo["user{$field}token"]) {
//                return json(['status' => 0, 'msg' => '此号码已经绑定了同类第三方']);
//            }
        }
//        $startTime = strtotime(date('Y-m-d 00:00:00'));//当天的起始时间
//        $endTime = strtotime(date('Y-m-d 23:59:59'));//当天的结束时间
//        $where = [];
//        $where['phone'] = $params['phone'];
//        $where['time2'] = ['between', [$startTime, $endTime]];
//        $num = Db::table('up_phonecode')->where($where)->count('id');
//        if($num == 5) {
//            return json(['status' => 0, 'msg' => '短信验证码今日已发送5次，请明天再来']);
//        }
        $china_phone = $params['phone'];
        if($userInfo && $userInfo['user_china'] != 86){
            $china_phone = '00' . $userInfo['user_china'] . $params['phone'];
        }
        $code = GetRandCode();//6位数验证码
        $templateid = config('yzx')['templateid'];
        if(isset($params['ex']) && $params['ex'] != '86'){
            $china_phone = '00' . $params['ex'] . $params['phone'];
        }
        $sendres = YzxSms($code, $china_phone, $templateid);//发送验证码
        if($sendres['code'] != '000000') {//发送不成功
            return json(['status' => 0, 'msg' => $sendres['msg']]);
        } else {
            $phone = $params['phone'];
            $isset = Db::table('up_phonecode')->where('phone', $phone)->find();
            $data = [
                'phone' => $params['phone'],//发送的手机号
                'code' => $code,//加密后的验证码
                'time2' => time(),//验证码发送的时间戳
                'status' => 0,//验证码发送的时间戳
                'create_time' => date('Y-m-d H:i:s')
            ];
            if ($isset) {
                $res = Db::table('up_phonecode')->where('phone', $phone)->update($data);
            } else {
                $res = Db::table('up_phonecode')->insert($data);
            }
            if(!$res) {
                return json(['status' => 0, 'msg' => '数据库操作失败']);
            }
            return json(['status' => 200, 'msg' => $code]);
        }
    }
    /**
     * 获取视频广告图
     */
    public function getVideoPic()
    {
        $field = 'lunbo_id,lunbo_name,lunbo_type,lunbo_image';
        $data = Db::table('up_advert_video')
            ->where('lunbo_type',2)
            ->where('lunbo_status',1)
            ->order('lunbo_px asc,lunbo_update_time desc')
            ->field($field)
            ->page(1,1)
            ->column('lunbo_image');
        $type = 1;
        if($data){
            $type = 2;
        }else{
            $data = Db::table('up_advert_video')
                ->where('lunbo_type',1)
                ->where('lunbo_status',1)
                ->order('lunbo_px asc,lunbo_update_time desc')
                ->field($field)
                ->page(1,5)
                ->column('lunbo_image');
        }
        return json(['status' => 200, 'msg' => $data, 'type' => $type]);
    }
}